const SkipList = require("../template")

function testSkipListRangeQueries() {
  const sl = new SkipList()

  console.log(
    "1. 范围查询基础测试:",
    (() => {
      sl.insert(1)
      sl.insert(3)
      sl.insert(5)
      sl.insert(7)
      sl.insert(9)
      const values = sl.getRangeValues(3, 7)
      const expected = [3, 5, 7]
      return JSON.stringify(values.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "2. 范围查询边界测试:",
    (() => {
      const values1 = sl.getRangeValues(1, 1)
      const values2 = sl.getRangeValues(9, 9)
      const values3 = sl.getRangeValues(0, 10)
      return values1.length === 1 && values2.length === 1 && values3.length === 5
    })()
  )

  console.log(
    "3. 范围查询空结果测试:",
    (() => {
      const values = sl.getRangeValues(10, 15)
      return values.length === 0
    })()
  )

  console.log(
    "4. 排名查询测试:",
    (() => {
      const rank1 = sl.getRank(1)
      const rank3 = sl.getRank(3)
      const rank5 = sl.getRank(5)
      const rank7 = sl.getRank(7)
      const rank9 = sl.getRank(9)
      return rank1 === 1 && rank3 === 2 && rank5 === 3 && rank7 === 4 && rank9 === 5
    })()
  )

  console.log(
    "5. 排名查询不存在元素测试:",
    (() => {
      const rank2 = sl.getRank(2)
      const rank6 = sl.getRank(6)
      const rank10 = sl.getRank(10)
      return rank2 === -1 && rank6 === -1 && rank10 === -1
    })()
  )

  console.log(
    "6. this上下文范围查询测试:",
    (() => {
      const slObj = {
        multiplier: 2,
        processRangeQuery: function(values, min, max) {
          const sl = new SkipList()
          values.forEach(val => sl.insert(val))
          const rangeValues = sl.getRangeValues(min, max)
          return rangeValues.length * this.multiplier
        }
      }
      return slObj.processRangeQuery([1, 2, 3, 4, 5], 2, 4) === 6 // 3*2=6
    })()
  )

  console.log(
    "7. 复杂对象this绑定范围查询测试:",
    (() => {
      const slConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(values, min, max) {
          const sl = new SkipList()
          values.forEach(val => sl.insert(val))
          const rangeValues = sl.getRangeValues(min, max)
          return rangeValues.length >= this.config.threshold
        }
      }
      return slConfigObj.processWithConfig([1, 2, 3, 4, 5], 2, 4) === true
    })()
  )

  console.log(
    "8. 范围查询性能测试:",
    (() => {
      const perfSl = new SkipList()
      const values = Array.from({ length: 1000 }, (_, i) => i + 1)
      values.forEach(val => perfSl.insert(val))
      
      const start = performance.now()
      for (let i = 0; i < 100; i++) {
        perfSl.getRangeValues(100, 900)
      }
      const end = performance.now()
      return (end - start) < 1000
    })()
  )

  console.log(
    "9. 范围查询一致性测试:",
    (() => {
      const consistencySl = new SkipList()
      consistencySl.insert(1)
      consistencySl.insert(3)
      consistencySl.insert(5)
      consistencySl.insert(7)
      consistencySl.insert(9)
      
      const values1 = consistencySl.getRangeValues(1, 9)
      const values2 = consistencySl.getAllValues()
      const rank1 = consistencySl.getRank(1)
      const rank9 = consistencySl.getRank(9)
      
      return values1.length === 5 && values2.length === 5 && rank1 === 1 && rank9 === 5
    })()
  )

  console.log(
    "10. 范围查询复杂测试:",
    (() => {
      const complexSl = new SkipList()
      const values = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
      values.forEach(val => complexSl.insert(val))
      
      const range1 = complexSl.getRangeValues(5, 15)
      const range2 = complexSl.getRangeValues(1, 5)
      const range3 = complexSl.getRangeValues(15, 19)
      
      return range1.length === 6 && range2.length === 3 && range3.length === 3
    })()
  )
}

testSkipListRangeQueries()
